const fs = require('fs');
const path = require('path');
const https = require('https');

const images = {
  'about-header.jpg': 'https://images.pexels.com/photos/3184465/pexels-photo-3184465.jpeg',
  'contact-header.jpg': 'https://images.pexels.com/photos/3184460/pexels-photo-3184460.jpeg',
  'services-header.jpg': 'https://images.pexels.com/photos/3183150/pexels-photo-3183150.jpeg',
  'team-header.jpg': 'https://images.pexels.com/photos/3184291/pexels-photo-3184291.jpeg',
  'join-us.jpg': 'https://images.pexels.com/photos/3184292/pexels-photo-3184292.jpeg',
  'company-intro.jpg': 'https://images.pexels.com/photos/3184296/pexels-photo-3184296.jpeg'
};

const teamImages = {
  'ceo.jpg': 'https://images.pexels.com/photos/2379004/pexels-photo-2379004.jpeg',
  'cto.jpg': 'https://images.pexels.com/photos/2182970/pexels-photo-2182970.jpeg',
  'coo.jpg': 'https://images.pexels.com/photos/1181686/pexels-photo-1181686.jpeg'
};

// 创建目录
const assetsDir = path.join(__dirname, '..', 'src', 'assets');
const teamDir = path.join(assetsDir, 'team');

if (!fs.existsSync(assetsDir)) {
  fs.mkdirSync(assetsDir, { recursive: true });
}
if (!fs.existsSync(teamDir)) {
  fs.mkdirSync(teamDir, { recursive: true });
}

// 下载图片函数
function downloadImage(url, filePath) {
  return new Promise((resolve, reject) => {
    https.get(url, (response) => {
      if (response.statusCode === 200) {
        const fileStream = fs.createWriteStream(filePath);
        response.pipe(fileStream);
        fileStream.on('finish', () => {
          fileStream.close();
          console.log(`Downloaded: ${filePath}`);
          resolve();
        });
      } else {
        reject(new Error(`Failed to download ${url}: ${response.statusCode}`));
      }
    }).on('error', (err) => {
      reject(err);
    });
  });
}

// 下载所有图片
async function downloadAllImages() {
  try {
    // 下载普通图片
    for (const [filename, url] of Object.entries(images)) {
      const filePath = path.join(assetsDir, filename);
      await downloadImage(url, filePath);
    }

    // 下载团队图片
    for (const [filename, url] of Object.entries(teamImages)) {
      const filePath = path.join(teamDir, filename);
      await downloadImage(url, filePath);
    }

    console.log('All images downloaded successfully!');
  } catch (error) {
    console.error('Error downloading images:', error);
  }
}

downloadAllImages(); 